//
//  CZMainTabBar.swift
//  wuJunQianWeiBo
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 wujunqian. All rights reserved.
//

import UIKit

class CZMainTabBar: UITabBar
{
    //按钮的个数
    private let count = 5
    /**
    *  设置按钮的frame
    */
    override func layoutSubviews()
    {
        super.layoutSubviews()
        //计算宽度,总宽度／个数
        let width = self.frame.width / CGFloat(count)
        //最左边按钮的frame
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        //遍历所有的子控件
        var index = 0
        for view in self.subviews
        {
            //判断如果是UITabBarButton才执行
            if view is UIControl
            {
                //重新设置frame，用偏移值
                view.frame = CGRectOffset(frame, width * CGFloat(index), 0)
                  index++
                if index == 2
                {
                    index++
                }
            }
        }
        //设置加号的frame,用偏移值
        self.conposeButton.frame = CGRectOffset(frame, width * 2.0, 0)
    }
    //懒加载button
    lazy var conposeButton:UIButton =
    {
        //创建button
        let button = UIButton()
        //设置背景图片,tabbar_compose_icon_add_highlighted
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"),forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //添加到tabBar
        self.addSubview(button)
        return button
    }()
}
